# from curl_cffi import requests
#
# urls = [f'https://www.mashangpa.com/api/problem-detail/3/data/?page={t}' for t in range(1,21)]
# headers = {
# 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
# 'referer':'https://www.mashangpa.com/problem-detail/2/',
#     'cookie':'Hm_lvt_0d2227abf9548feda3b9cb6fddee26c0=1762344196; HMACCOUNT=B7FBCF2D05FEB3F2; sessionid=xxs7z5sywxfasnz8dsl2vthknuy2sv0u; Hm_lpvt_0d2227abf9548feda3b9cb6fddee26c0=1762349661'
# }
#
# result = 0
# for url in urls:
#     res = requests.get(url,headers=headers)
#     content = res.json()['current_array']
#     total = 0
#     for item in content:
#        total += item
#     print(total)
#     result += total
# print(result)



# import time
# import requests
# import hashlib
#
# # 目标接口基础URL（不含参数）
# base_url = 'https://www.mashangpa.com/api/problem-detail/4/data/'
# headers = {
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
#     'referer': 'https://www.mashangpa.com/problem-detail/2/',
#     'cookie': 'Hm_lvt_0d2227abf9548feda3b9cb6fddee26c0=1762344196; HMACCOUNT=B7FBCF2D05FEB3F2; sessionid=xxs7z5sywxfasnz8dsl2vthknuy2sv0u; Hm_lpvt_0d2227abf9548feda3b9cb6fddee26c0=1762349661'
# }
#
# # 假设的加密密钥（请根据实际破解结果修改，此处为示例）
# fixed_key = "tuling"  # 关键：需替换为正确的密钥
#
# result = 0
#
# # 爬取第1到20页
# for page in range(1, 21):
#     try:
#         # 1. 生成当前时间戳（13位）
#         _ts = str(int(time.time() * 1000))
#
#         # 2. 动态生成sign（按实际加密规则拼接字符串）
#         # 注意：加密字符串的组合顺序（page、_ts、key的顺序）需与网站一致
#         sign_str = f"{fixed_key}{_ts}{page}"  # 示例规则：key + page + _ts
#         sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()  # MD5加密
#
#         # 3. 构造请求参数
#         params = {
#             'page': page,
#             'sign': sign,
#             '_ts': _ts
#         }
#
#         # 4. 发送请求（使用基础URL + 动态参数）
#         res = requests.get(
#             url=base_url,
#             params=params,
#             headers=headers,
#             timeout=10
#         )
#         res.raise_for_status()  # 检查HTTP错误状态码
#
#         # 5. 解析数据并累加
#         content = res.json()['current_array']
#         page_total = sum(content)  # 直接用sum()简化累加
#         print(f"第{page}页总和：{page_total}")
#         result += page_total
#
#         # 控制爬取速度，避免反爬
#         time.sleep(1)
#
#     except Exception as e:
#         print(f"第{page}页爬取失败：{e}")
#         # 可选择继续爬取下一页或退出
#         continue
#
# print(f"\n所有页面总和：{result}")


import requests
import time
import json
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

base_url = 'https://www.mashangpa.com/api/problem-detail/5/data/'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
    'referer': 'https://www.mashangpa.com/problem-detail/5/',
    'cookie': 'sessionid=xxs7z5sywxfasnz8dsl2vthknuy2sv0u; Hm_lvt_0d2227abf9548feda3b9cb6fddee26c0=1762344196,1762421178; HMACCOUNT=B7FBCF2D05FEB3F2; Hm_lpvt_0d2227abf9548feda3b9cb6fddee26c0=1762428242',
    'Content-Type': 'application/json'
}

# 真实密钥和IV（从前端解析）
KEY = b'jo8j9wGw%6HbxfFn'  # 16字节
IV = b'0123456789ABCDEF'  # 16字节


def encrypt_data(data):
    cipher = AES.new(KEY, AES.MODE_CBC, IV)
    padded_data = pad(data.encode('utf-8'), AES.block_size, style='pkcs7')
    encrypted = cipher.encrypt(padded_data)
    # 核心修正：加密结果转为16进制字符串（与前端一致）
    return encrypted.hex()  # 替代base64.b64encode


result = 0
for page in range(1, 21):
    try:
        _ts = str(int(time.time() * 1000))
        raw_params = {'page': page, '_ts': _ts}
        json_str = json.dumps(raw_params)
        encrypted = encrypt_data(json_str)
        payload = json.dumps({'xl': encrypted})  # 键名仍为xl

        response = requests.post(
            url=base_url,
            data=payload,
            headers=headers,
            timeout=10
        )
        print(f"第{page}页响应状态：{response.status_code}")
        

        response.raise_for_status()
        data = response.json()
        current_array = data['current_array']
        page_total = sum(current_array)
        result += page_total
        print(f"第{page}页总和：{page_total}")
        time.sleep(1)
    except Exception as e:
        print(f"第{page}页失败：{str(e)}")

print(f"最终总和：{result}")